#include "modisutitls.h"

void modisUtils_getRadianceScaleAndOffset(string fulldsname, vector<double>& scaleArr, vector<double>& offsetArr)
{
    GDALDataset* ds1 = (GDALDataset*)GDALOpen(fulldsname.c_str(), GA_ReadOnly);
    if (ds1 == 0) return;
    char** SUBDATASETS = GDALGetMetadata(
        (GDALDatasetH)ds1, "");
    if (CSLCount(SUBDATASETS) > 0)
    {
        for (int i = 0; SUBDATASETS[i] != NULL; i++)
        {
            string tmpstr = string(SUBDATASETS[i]);
            if (tmpstr.find("radiance_scales")
                != std::string::npos) {
                vector<string> sarr1 =
                    wStringUtils::splitString(tmpstr, "=");
                if (sarr1.size() == 2) {
                    vector<string> sarr2 =
                        wStringUtils::splitString(sarr1[1], ",");
                    for (int is = 0; is < sarr2.size(); ++is) {
                        scaleArr.push_back(std::stod(sarr2[is]));
                    }
                }
            }
            if (tmpstr.find("radiance_offsets")
                != std::string::npos) {
                vector<string> sarr1 =
                    wStringUtils::splitString(tmpstr, "=");
                if (sarr1.size() == 2) {
                    vector<string> sarr2 =
                        wStringUtils::splitString(sarr1[1], ",");
                    for (int is = 0; is < sarr2.size(); ++is) {
                        offsetArr.push_back(std::stod(sarr2[is]));
                    }
                }
            }
        }
    }
    GDALClose(ds1);
}
void modisUtils_getReflectanceScaleAndOffset(string fulldsname, vector<double>& scaleArr, vector<double>& offsetArr)
{
    GDALDataset* ds1 = (GDALDataset*)GDALOpen(fulldsname.c_str(), GA_ReadOnly);
    if (ds1 == 0) return;
    char** SUBDATASETS = GDALGetMetadata(
        (GDALDatasetH)ds1, "");
    if (CSLCount(SUBDATASETS) > 0)
    {
        for (int i = 0; SUBDATASETS[i] != NULL; i++)
        {
            string tmpstr = string(SUBDATASETS[i]);
            if (tmpstr.find("reflectance_scales")
                != std::string::npos) {
                vector<string> sarr1 =
                    wStringUtils::splitString(tmpstr, "=");
                if (sarr1.size() == 2) {
                    vector<string> sarr2 =
                        wStringUtils::splitString(sarr1[1], ",");
                    for (int is = 0; is < sarr2.size(); ++is) {
                        scaleArr.push_back(std::stod(sarr2[is]));
                    }
                }
            }
            if (tmpstr.find("reflectance_offsets")
                != std::string::npos) {
                vector<string> sarr1 =
                    wStringUtils::splitString(tmpstr, "=");
                if (sarr1.size() == 2) {
                    vector<string> sarr2 =
                        wStringUtils::splitString(sarr1[1], ",");
                    for (int is = 0; is < sarr2.size(); ++is) {
                        offsetArr.push_back(std::stod(sarr2[is]));
                    }
                }
            }
        }
    }
    GDALClose(ds1);
}